const db = wx.cloud.database()
var times = require('../../utils/times.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        mglist: '',
        test_calendar: '',
        LemSpecial: '',
        books_list: '',
        currentSwiperIndex: 0,
        windowHeight: 0,
        pageIndex: 0,
        pageSize: 4,
        triggered: false,
    },

    //获取轮播图
    getbanner() {
        db.collection("banner").get({
            success: res => {
                wx.showToast({
                    title: '加载成功！',
                })
                console.log(res, '轮播图加载成功！');
                this.setData({
                    mglist: res.data
                })
            }
        })
    },
    //获取考试日历
    gettest() {
        db.collection("test_calendar").get({
            success: res => {
                wx.showToast({
                    title: '加载成功！',
                })
                console.log(res, '考试日历加载成功！')
                for (var i = 0; i < res.data.length; i++) {
                    res.data[i]["test_apply"] = times.toDate(res.data[i]["test_apply"])
                    res.data[i]["test_time"] = times.toDate(res.data[i]["test_time"])
                }
                this.setData({
                    test_calendar: res.data
                })
            }
        })
    },
    //获取每周一听专题
    getLemSpecial() {
        db.collection("LemSpecial").get({
            success: res => {
                wx.showToast({
                    title: '加载成功！',
                })
                console.log(res, '每周一听加载成功！')
                this.setData({
                    LemSpecial: res.data,
                })
            }
        })
    },
    //获取书籍列表
    getbooks_list() {
        const { pageSize, pageIndex, books_list } = this.data
        wx.showLoading({
            title: '加载中....',
        })
        db.collection("books_list")
            .limit(pageSize)
            .skip((pageIndex - 1) * pageSize).orderBy('_createTime', 'desc')
            .get({
                success: res => {
                    wx.showToast({
                        title: '加载成功！',
                    })
                    this.setData({
                        books_list: Array.from(new Set([...books_list, ...res.data].map(item => JSON.stringify(item)))).map(item => JSON.parse(item)),
                        pageSize,
                        pageIndex,
                    })
                }
            })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getbanner()
        this.gettest()
        this.getLemSpecial()
        this.getbooks_list()
        const { windowHeight } = wx.getWindowInfo()
        this.setData({
            windowHeight,
        })
    },
    //跳转到座位预约
    Seat_Reservation() {
        wx.navigateTo({
            url: '../Seat_Reservation/Seat_Reservation',
        })
    },
    //跳转到签到打卡
    SignIn(e) {
        let user = wx.getStorageSync('user')
        if (!user == '') {
            wx.navigateTo({
                url: '../SignIn/SignIn',
            })
        } else {
            wx.showToast({
                title: '请先登录！',
                icon: 'none'
            })
        }
    },
    //跳转到学习书法
    Learn_calligraphy() {
        wx.showToast({
            title: '正在开发中...',
            icon: 'none'
        })
    },
    //跳转到搜索中心
    search(e) {
        wx.navigateTo({
            url: '../search/search',
        })
    },
    //跳转到每周一听
    Lem() {
        wx.navigateTo({
            url: '../Lem/Lem',
        })
    },
    //跳转到分类页面
    ClassIfication() {
        wx.navigateTo({
            url: '../ClassIfication/ClassIfication',
        })
    },
    //跳转到考试日历
    test_calendar() {
        wx.navigateTo({
            url: '../test_calendar/test_calendar',
        })
    },
    //跳转到图书详情
    book_details: function (e) {
        console.log(e.currentTarget.id)
        wx.navigateTo({
            url: '../book_details/book_details?book_id=' + e.currentTarget.id,
        })
    },
    //跳转到听书详情
    Hearing_details: function (e) {
        console.log(e.currentTarget.id)
        wx.navigateTo({
            url: '../Hearing_details/Hearing_details?Hearing_id=' + e.currentTarget.id,
        })
    },
    //跳转到听书专题详情
    LemSpecial: function (e) {
        console.log(e.currentTarget.id)
        wx.navigateTo({
            url: '../LemSpecial_details/LemSpecial_details?LemSpecial_id=' + e.currentTarget.id,
        })
    },
    //轮播图
    onSwiperChange(e) {
        const { current } = e.detail
        this.setData({
            currentSwiperIndex: current
        })
    },
    scrolltolower() {
        this.setData({
            pageIndex: this.data.pageIndex += 1
        }, () => {
            this.getbooks_list()
        })
    },
    //复制网址
    copyBtn: function (e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.text,
            success: function (res) {
                wx.getClipboardData({
                    success: function (res) {
                        wx.showToast({
                            title: '复制成功'
                        })
                    }
                })
            }
        })
    },
    //跳转到考试公考
    test_announcement(e) {
        console.log(e.currentTarget.id)
        wx.navigateTo({
            url: '../test_announcement/test_announcement?announcement_id=' + e.currentTarget.id,
        })
    },
    refresherrefresh() {
        this.getbanner()
        this.gettest()
        this.getLemSpecial()
        this.getbooks_list()
        setTimeout(() => {
            this.setData({ triggered: false })
        }, 700);
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})